---
title: "Replication material for 'Why Do Citizens (Not) Support Democratic Innovations? The Role of Instrumental Motivations in Support for Participatory Budgeting'"

author: | 
        | Ramon van der Does
        | Jaroslaw Kantorowicz

date: |
      | January 18, 2021
      
output: html_notebook
---

# Installing/loading required packages

```{r}

#install.packages("cregg")
#install.packages("cjoint")
#install.packages("tidyverse")
#devtools::install_github("jaredhuling/jcolors")
#install.packages("ggpubr")
#install.packages("arm")
#install.packages("GGally")
#install.packages("grid")
#install.packages("xlsx")
#install.packages("quanteda")
#install.packages("topicmodels")

library(cregg)
library(cjoint)
library(tidyverse)
library(jcolors)
library(ggpubr)
library(arm)
library(GGally)
library(grid)
library(quanteda)
library(topicmodels)

dark2 <- c("#1B9E77", "#D95F02", "#7570B3", "#E7298A", "#66A61E", "#E6AB02", "#A6761D", "#666666")

```


# Load data for study 1

```{r}

rat <- read.csv(file="study_1.csv")

rat$residents_allowed_submit <- as.factor(rat$residents_allowed_submit)
rat$projects_allowed <- as.factor(rat$projects_allowed)
rat$support_municipality <- as.factor(rat$support_municipality)
rat$choice_winning_project <- as.factor(rat$choice_winning_project)
rat$outcome_favorability <- as.factor(rat$outcome_favorability)

```

# Load data for study 2

```{r}

rat_distance_final <- read.csv(file="study_2.csv")

rat_distance_final$residents_allowed_submit <- as.factor(rat_distance_final$residents_allowed_submit)
rat_distance_final$projects_allowed <- as.factor(rat_distance_final$projects_allowed)
rat_distance_final$support_municipality <- as.factor(rat_distance_final$support_municipality)
rat_distance_final$choice_winning_project <- as.factor(rat_distance_final$choice_winning_project)
rat_distance_final$project_location <- as.factor(rat_distance_final$project_location)

```

# Load data for study 3

```{r}

rat_efficiency_final <- read.csv(file="study_3.csv")

rat_efficiency_final$residents_allowed_submit <- as.factor(rat_efficiency_final$residents_allowed_submit)
rat_efficiency_final$projects_allowed <- as.factor(rat_efficiency_final$projects_allowed)
rat_efficiency_final$support_municipality <- as.factor(rat_efficiency_final$support_municipality)
rat_efficiency_final$choice_winning_project <- as.factor(rat_efficiency_final$choice_winning_project)
rat_efficiency_final$outcome_favorability <- as.factor(rat_efficiency_final$outcome_favorability)
rat_efficiency_final$final_cost <- as.factor(rat_efficiency_final$final_cost)

```


# Figure A.1.

```{r}

discrete.histogram(rat$selected, xlab="Rating", prob.col="gray")

```

# Load data for study 1, forced choice dependent variable

```{r}

fc <- read.csv(file="study_1_fc.csv")

fc$residents_allowed_submit <- as.factor(fc$residents_allowed_submit)
fc$projects_allowed <- as.factor(fc$projects_allowed)
fc$support_municipality <- as.factor(fc$support_municipality)
fc$choice_winning_project <- as.factor(fc$choice_winning_project)
fc$outcome_favorability <- as.factor(fc$outcome_favorability)

```


# Figure A.2

```{r}

# Formally testing for the presence of carry-over effect

cj_anova(fc, selected ~ 
             residents_allowed_submit +
             projects_allowed +
             support_municipality + 
             choice_winning_project +
             outcome_favorability, 
             by = ~task, 
             id = ~responseid)


# Figure

fc$task <- as.factor(fc$task)

fc_task_number <-cj(fc, 
                    selected ~ 
                    residents_allowed_submit +
                    projects_allowed +
                    support_municipality + 
                    choice_winning_project +
                    outcome_favorability, 
                    id = ~responseid,
                    by = ~task,
                    estimate="mm",
                    feature_order = c("residents_allowed_submit",
                                      "projects_allowed",
                                      "support_municipality",
                                      "choice_winning_project",
                                      "outcome_favorability"),
                    feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                       projects_allowed = "What projects are allowed?",
                                       outcome_favorability = "Is the outcome favorable?", 
                                       support_municipality = "Does the muninicipality offer any support?", 
                                       choice_winning_project="Who is choosing the winning project?"))

        
    
fc_task_number_plot <- plot(fc_task_number, 
                            vline=0.5, 
                            size=2, 
                            group="task",
                            feature_headers = FALSE,)


fc_task_number_plot_final <- fc_task_number_plot +
           theme(legend.position="bottom") + 
           theme(axis.text.y = element_text(colour = "black")) +
           theme(axis.text.x = element_text(colour = "black")) +
           theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
           theme(text = element_text(size=15))  +
           ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") 

        
plot(fc_task_number_plot_final)

fc_task_number_plot_final %>% ggsave(file="figure_a2.png",
                                     width = 8, 
                                     height = 8, 
                                     dpi = 150, 
                                     units = "in", 
                                     device='png')

```

# Figure A.3

```{r}
fc <- fc %>% mutate(profile2=ifelse(profile==1, 
                                    "Left", 
                                    "Right"))
        
fc$profiles <- fc$profile2

# Formally testing for the presence of profile-order effect

cj_anova(fc, selected ~ 
             residents_allowed_submit +
             projects_allowed +
             support_municipality + 
             choice_winning_project +
             outcome_favorability, 
             by = ~profile, 
             id = ~responseid)

fc$profiles <- as.factor(fc$profiles)
        
fc_profile_order <- cj(fc, 
                       selected ~ 
                       residents_allowed_submit +
                       projects_allowed +
                       support_municipality + 
                       choice_winning_project +
                       outcome_favorability, 
                       id = ~responseid,
                       by = ~profiles,
                       estimate="mm",
                       feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability"),
                       feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?"))

        
    
fc_profile_order_plot <- plot(fc_profile_order, 
                              vline=0.5, 
                              size=2, 
                              group="profiles",
                              feature_headers = FALSE,)


fc_profile_order_plot_final <- fc_profile_order_plot +
           theme(legend.position="bottom") + 
           theme(axis.text.y = element_text(colour = "black")) +
           theme(axis.text.x = element_text(colour = "black")) +
           theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
           theme(text = element_text(size=15))  +
           ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") 

plot(fc_profile_order_plot_final)

fc_profile_order_plot_final %>% ggsave(file="figure_a3.png",
                                       width = 8, 
                                       height = 8, 
                                       dpi = 150, 
                                       units = "in", 
                                       device='png')

```



# Figure A.4

```{r}

fc$support_municipality <- factor(fc$support_municipality, 
                                  levels= c("No extra support", 
                                            "Organizing discussions", 
                                            "Helping to develop the project"))


fc_amce <- cregg::cj(fc,
              selected ~ 
              residents_allowed_submit + 
              projects_allowed + 
              support_municipality + 
              choice_winning_project + 
              outcome_favorability, 
              id = ~responseid, 
              estimate = "amce",
              feature_order = c("residents_allowed_submit",
                                "projects_allowed",
                                "support_municipality",
                                "choice_winning_project",
                                "outcome_favorability"),
              feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                 projects_allowed = "What projects are allowed?",
                                 outcome_favorability = "Is the outcome favorable?", 
                                 support_municipality = "Does the muninicipality offer any support?", 
                                 choice_winning_project="Who is choosing the winning project?"))

plot_fc_amce <- plot(fc_amce, 
                     vline=0.0, 
                     size=1, 
                     feature_headers = FALSE)


plot_fc_amce_final <- plot_fc_amce +
           theme(legend.position="none") + 
           theme(axis.text.y = element_text(colour = "black")) +
           theme(axis.text.x = element_text(colour = "black")) +
           theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
           theme(text = element_text(size=15))  +
           ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") +
           geom_text(subset(fc_amce, estimate!=0), mapping=aes(label=round(estimate,3)), 
                     parse = TRUE, color="black", size=3, check_overlap = TRUE, 
                     vjust = 1.4) +scale_color_jcolors("pal11") +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_blank(),
        panel.border = element_rect(colour = "black")) +
        labs(x = "Estimated AMCE", y = "")
            

plot(plot_fc_amce_final)

plot_fc_amce_final %>% ggsave(filename = "figure_a4.png",
                              width = 8, 
                              height = 8, 
                              dpi = 150, 
                              units = "in", 
                              device='png')

```

# Figure A.5

```{r}

fc_task_1 <- fc %>% filter(task==1)

# Formally testing for the presence of profile-order effect for the first task only

cj_anova(fc_task_1, selected ~ 
                    residents_allowed_submit +
                    projects_allowed +
                    support_municipality + 
                    choice_winning_project +
                    outcome_favorability, 
                    by = ~profile, 
                    id = ~responseid)


fc_profile_order_task_1 <- cj(fc_task_1, 
                                selected ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability, 
                                id = ~responseid,
                                by = ~profiles,
                                estimate="mm",
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?"))

        
    
fc_profile_order_task_1_plot <- plot(fc_profile_order_task_1, 
                                     vline=0.5, 
                                     size=2, 
                                     group="profiles",
                                     feature_headers = FALSE,)


fc_profile_order_task_1_plot_final <- fc_profile_order_task_1_plot +
           theme(legend.position="bottom") + 
           theme(axis.text.y = element_text(colour = "black")) +
           theme(axis.text.x = element_text(colour = "black")) +
           theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
           theme(text = element_text(size=15))  +
           ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") 

plot(fc_profile_order_task_1_plot_final)

fc_profile_order_task_1_plot_final %>% ggsave(file="figure_a5.png",
                                              width = 8, 
                                              height = 8, 
                                              dpi = 150, 
                                              units = "in", 
                                              device='png')

```

# Figure A.6

```{r}
fc_amce <- cj(fc_task_1,
              selected ~ 
              residents_allowed_submit + 
              projects_allowed + 
              support_municipality + 
              choice_winning_project + 
              outcome_favorability +
              profiles, 
              id = ~responseid, 
              estimate = "amce",
              feature_order = c("residents_allowed_submit",
                                "projects_allowed",
                                "support_municipality",
                                "choice_winning_project",
                                "outcome_favorability",
                                "profiles"),
              feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                 projects_allowed = "What projects are allowed?",
                                 outcome_favorability = "Is the outcome favorable?", 
                                 support_municipality = "Does the muninicipality offer any support?", 
                                 choice_winning_project="Who is choosing the winning project?",
                                profiles="Profile"))

plot_fc_amce <- plot(fc_amce, 
                     vline=0.0, 
                     size=1, 
                     feature_headers = FALSE)

plot_fc_amce_final <- plot_fc_amce +
           theme(legend.position="none") + 
           theme(axis.text.y = element_text(colour = "black")) +
           theme(axis.text.x = element_text(colour = "black")) +
           theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
           theme(text = element_text(size=15))  +
           ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") +
           geom_text(subset(fc_amce, estimate!=0), mapping=aes(label=round(estimate,3)), 
                     parse = TRUE, color="black", size=3, check_overlap = TRUE, 
                     vjust = 1.4) +
           scale_color_jcolors("pal11") +
        theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_blank(),
        panel.border = element_rect(colour = "black")) +
        labs(y = "", x = "Estimated AMCE")
            

plot(plot_fc_amce_final)

plot_fc_amce_final

plot_fc_amce_final %>% ggsave(filename = "figure_a6.png",
                              width = 8, 
                              height = 8, 
                              dpi = 150, 
                              units = "in", 
                              device='png')

```

# Figure A.7

```{r}
fc <- fc %>% mutate(mean = (voetpaden +
                          fietspaden +
                          speeltuinen +
                          pleinen + 
                          verlichting + 
                          verkeersdrempels +
                          bosjes + 
                          vuilnisbakken)/8)

fc <- fc %>% mutate(sd=sqrt(((voetpaden-mean)^2 +
                              (fietspaden-mean)^2 +
                              (speeltuinen-mean)^2 +
                              (pleinen-mean)^2 +
                              (verlichting-mean)^2 +
                              (verkeersdrempels-mean)^2 +
                              (bosjes-mean)^2 +
                              (vuilnisbakken-mean)^2)/8))


fc
hist(fc$sd)
hist(fc$mean)

fc <- fc %>% mutate(sd_1=ifelse( sd>=1, 1, 0))
fc <- fc %>% mutate(sd_2=ifelse( sd>=2, 1, 0))

# Figure in Panel A: standard deviation >=1

fc_sd_1 <- fc %>% filter(sd_1==1)

fc_amce <- cj(fc_sd_1,
              selected ~ 
                      residents_allowed_submit + 
                      projects_allowed + 
                      support_municipality + 
                      choice_winning_project + 
                      outcome_favorability, 
                      id = ~responseid, 
                      estimate = "amce",
                      feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability"),
                      feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?"))


plot_fc_amce <- plot(fc_amce, 
                     vline=0.0, 
                     size=1, 
                     feature_headers = FALSE)

plot_fc_amce_final <- plot_fc_amce +
           theme(legend.position="none") + 
           theme(axis.text.y = element_text(colour = "black")) +
           theme(axis.text.x = element_text(colour = "black")) +
           theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
           theme(text = element_text(size=15))  +
           ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") +
           geom_text(subset(fc_amce, estimate!=0), mapping=aes(label=round(estimate,3)), 
                     parse = TRUE, color="black", size=3, check_overlap = TRUE, 
                     vjust = 1.4) +scale_color_jcolors("pal11") +
        theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_blank(),
        panel.border = element_rect(colour = "black")) +
        labs(x = "Estimated AMCE", y = "")
            

plot(plot_fc_amce_final)

plot_fc_amce_final %>% ggsave(filename = "figure_a7a.png",
                              width = 8, 
                              height = 8, 
                              dpi = 150, 
                              units = "in", 
                              device='png')

# Figure in Panel B: standard deviation >=2

fc_sd_2 <- fc %>% filter(sd_2==1)

fc_amce <- cj(fc_sd_2,
              selected ~ 
              residents_allowed_submit + 
              projects_allowed + 
              support_municipality + 
              choice_winning_project + 
              outcome_favorability, 
              id = ~responseid, 
              estimate = "amce",
              feature_order = c("residents_allowed_submit",
                                "projects_allowed",
                                "support_municipality",
                                "choice_winning_project",
                                "outcome_favorability"),
              feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                 projects_allowed = "What projects are allowed?",
                                 outcome_favorability = "Is the outcome favorable?", 
                                 support_municipality = "Does the muninicipality offer any support?", 
                                 choice_winning_project="Who is choosing the winning project?"))

plot_fc_amce <- plot(fc_amce, 
                     vline=0.0, 
                     size=1, 
                     feature_headers = FALSE)

plot_fc_amce_final <- plot_fc_amce +
           theme(legend.position="none") + 
           theme(axis.text.y = element_text(colour = "black")) +
           theme(axis.text.x = element_text(colour = "black")) +
           theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
           theme(text = element_text(size=15))  +
           ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") +
           geom_text(subset(fc_amce, estimate!=0), mapping=aes(label=round(estimate,3)), 
                     parse = TRUE, color="black", size=3, check_overlap = TRUE, 
                     vjust = 1.4) +scale_color_jcolors("pal11") +
        theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_blank(),
        panel.border = element_rect(colour = "black")) +
        labs(x = "Estimated AMCE", y = "")
            

plot(plot_fc_amce_final)

plot_fc_amce_final %>% ggsave(filename = "figure_a7b.png",
                              width = 8, 
                              height = 8, 
                              dpi = 150, 
                              units = "in", 
                              device='png')

```

# Figure A.9

```{r}

rat$task <- as.factor(rat$task)

rat_task_number <-cj(rat, 
                     selected ~ 
                     residents_allowed_submit +
                     projects_allowed +
                     support_municipality + 
                     choice_winning_project +
                     outcome_favorability, 
                     id = ~responseid,
                     by = ~task,
                     estimate="mm",
                     feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability"),
                     feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?"))

        
    
rat_task_number_plot <- plot(rat_task_number, 
                             vline=0.5, 
                             size=2, 
                             group="task",
                             feature_headers = FALSE,)


rat_task_number_plot_final <- rat_task_number_plot +
           theme(legend.position="bottom") + 
           theme(axis.text.y = element_text(colour = "black")) +
           theme(axis.text.x = element_text(colour = "black")) +
           theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
           theme(text = element_text(size=15))  +
           ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") 

        
plot(rat_task_number_plot_final)

rat_task_number_plot_final %>% ggsave(file="figure_a9.png",
                                      width = 8, 
                                      height = 8, 
                                      dpi = 150, 
                                      units = "in", 
                                      device='png')

```

# Figure A.10


```{r}

rat_task_1 <- rat %>% filter(task==1)
    
rat_task_1 <- rat_task_1 %>% mutate(profile2=ifelse(profile==1, "Left", "Right"))
                
rat_task_1$profiles <- as.factor(rat_task_1$profile2)

rat_task_1 %>% group_by(profiles) %>% summarise(mean_outcome=mean(selected))
    
rat_mm_profiles_first_task <- cj(rat_task_1, 
                                selected ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability, 
                                id = ~responseid,
                                estimate = "mm",
                                by =  ~profiles,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?"))


plot_rat_mm_profiles_first_task <- plot(rat_mm_profiles_first_task,
                     size=1, 
                     feature_headers = FALSE,
                     by = "profiles")

plot_rat_mm_final_profiles_first_task <- plot_rat_mm_profiles_first_task +
           theme(legend.position="left") + 
           theme(axis.text.y = element_text(colour = "black")) +
           theme(axis.text.x = element_text(colour = "black")) +
           theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
           theme(text = element_text(size=15))  +
           ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") +
           geom_vline(xintercept = 4.704528, color="#1B9E77") +
           geom_vline(xintercept = 4.598619, color="#D95F02") +
           scale_color_manual(values = c("#1B9E77", "#D95F02"))

```


```{r}

rat_differences_profiles_first_task <- mm_diffs(rat_task_1, 
                                selected ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability, 
                                id = ~responseid,
                                h0=-0.105909,
                                by = ~profiles,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?"))

    
rat_differences_profiles_first_task_plot <- plot(rat_differences_profiles_first_task, 
                                                 vline=-0.105909, 
                                                 size=2, 
                                                 feature_headers = FALSE)


rat_differences_profiles_first_task_plot_final <- rat_differences_profiles_first_task_plot + 
                    theme(legend.position="none") +
                    theme(axis.text.y = element_text(colour = "black")) +
                    theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
                    theme(text = element_text(size=15)) +
                    ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") +
                    geom_text(rat_differences_profiles_first_task, 
                              mapping=aes(label=round(estimate,3)), 
                              parse = TRUE, color="black", size=3, check_overlap = TRUE, 
                              vjust = 1.4) +
                    scale_color_manual(values = dark2) +
                    scale_x_continuous(limits = c(-0.5, 0.3)) +
                    labs(x = "Estimated differences (Yes-No)", y = " ") +
                    theme(axis.title.x = element_text(size=12))

plot(rat_differences_profiles_first_task_plot_final)

figure_profiles_first_task <- ggarrange(plot_rat_mm_final_profiles_first_task,
                                        rat_differences_profiles_first_task_plot_final,
                                        ncol = 2, 
                                        widths = c(1.4,1))

plot(figure_profiles_first_task)

ggsave("figure_a10.png", 
       width = 18, 
       height = 8, 
       dpi = 150, 
       units = "in", 
       device='png')

```


# Figure A.11

```{r}

# Formally testing the profile order effect

cj_anova(rat, selected ~ 
              residents_allowed_submit +
              projects_allowed +
              support_municipality + 
              choice_winning_project +
              outcome_favorability, 
              by = ~profile, 
              id = ~responseid)

rat <- rat %>% mutate(profile2=ifelse(profile==1, 
                                      "Left", 
                                      "Right"))

rat$profile2 <- as.factor(rat$profile2)

rat$support_municipality <- factor(rat$support_municipality, 
                                   levels= c("No extra support", 
                                             "Organizing discussion", 
                                             "Helping to develop the project"))

rat_amce <- cj(rat,
              selected ~ 
                      residents_allowed_submit + 
                      projects_allowed + 
                      support_municipality + 
                      choice_winning_project + 
                      outcome_favorability +
                      profile2, 
                      id = ~responseid, 
                      estimate = "amce",
                      feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "profile2"),
                      feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         profile2="Profile"))


plot_rat_amce <- plot(rat_amce, 
                     vline=0.0, 
                     size=1, 
                     feature_headers = FALSE)


plot_rat_amce_final <- plot_rat_amce +
           theme(legend.position="none") + 
           theme(axis.text.y = element_text(colour = "black")) +
           theme(axis.text.x = element_text(colour = "black")) +
           theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
           theme(text = element_text(size=15))  +
           ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") +
           geom_text(subset(rat_amce, estimate!=0), mapping=aes(label=round(estimate,3)), 
                     parse = TRUE, color="black", size=3, check_overlap = TRUE, 
                     vjust = 1.4) +scale_color_jcolors("pal11") +
           theme(panel.grid.major = element_blank(),
           panel.grid.minor = element_blank(),
           strip.background = element_blank(),
           panel.border = element_rect(colour = "black")) +
           labs(x = "Estimated AMCE", y = "")
            

plot(plot_rat_amce_final)

plot_rat_amce_final

plot_rat_amce_final %>% ggsave(filename = "figure_a11.png",
                               width = 8, 
                               height = 8, 
                               dpi = 150, 
                               units = "in", 
                               device='png')

```


# Figure A.12

```{r}

# Append the weights (for generating the weights we used the STATA module: ipfweight)

weights <- read.csv("weights_final.csv", sep = ',')

rat_weights <- merge(rat, weights, by = 'responseid')

rat_amce <- cregg::cj(rat_weights,
              selected ~ 
              residents_allowed_submit + 
              projects_allowed + 
              support_municipality + 
              choice_winning_project + 
              outcome_favorability +
              profile2, 
              id = ~responseid, 
              estimate = "amce",
              weights = ~weights_final,
              feature_order = c("residents_allowed_submit",
                                "projects_allowed",
                                "support_municipality",
                                "choice_winning_project",
                                "outcome_favorability",
                                "profile2"),
              feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                 projects_allowed = "What projects are allowed?",
                                 outcome_favorability = "Is the outcome favorable?", 
                                 support_municipality = "Does the muninicipality offer any support?", 
                                 choice_winning_project="Who is choosing the winning project?",
                                profile2="Profile"))

plot_rat_amce <- plot(rat_amce, 
                     vline=0.0, 
                     size=1, 
                     feature_headers = FALSE)

plot_rat_amce_final <- plot_rat_amce +
           theme(legend.position="none") + 
           theme(axis.text.y = element_text(colour = "black")) +
           theme(axis.text.x = element_text(colour = "black")) +
           theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
           theme(text = element_text(size=15))  +
           ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") +
           geom_text(subset(rat_amce, estimate!=0), mapping=aes(label=round(estimate,3)), 
                     parse = TRUE, color="black", size=3, check_overlap = TRUE, 
                     vjust = 1.4) +scale_color_jcolors("pal11") +
        theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_blank(),
        panel.border = element_rect(colour = "black")) +
        labs(x = "Estimated AMCE", y = "")
            

plot(plot_rat_amce_final)

plot_rat_amce_final

plot_rat_amce_final %>% ggsave(filename = "figure_a12.png",
                               width = 8, 
                               height = 8, 
                               dpi = 150, 
                               units = "in", 
                               device='png')

```

# Figure A.13

```{r}

# (for generating the weights we used the STATA module: ipfweight)

rat_weights_task1 <- rat_weights %>% filter(task==1)

rat_amce <- cregg::cj(rat_weights_task1,
              selected ~ 
              residents_allowed_submit + 
              projects_allowed + 
              support_municipality + 
              choice_winning_project + 
              outcome_favorability, 
              id = ~responseid, 
              estimate = "amce",
              weights = ~weights_final,
              feature_order = c("residents_allowed_submit",
                               "projects_allowed",
                               "support_municipality",
                               "choice_winning_project",
                               "outcome_favorability"),
              feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                 projects_allowed = "What projects are allowed?",
                                 outcome_favorability = "Is the outcome favorable?", 
                                 support_municipality = "Does the muninicipality offer any support?", 
                                 choice_winning_project="Who is choosing the winning project?"))

plot_rat_amce <- plot(rat_amce, 
                     vline=0.0, 
                     size=1, 
                     feature_headers = FALSE)

plot_rat_amce_final <- plot_rat_amce +
           theme(legend.position="none") + 
           theme(axis.text.y = element_text(colour = "black")) +
           theme(axis.text.x = element_text(colour = "black")) +
           theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
           theme(text = element_text(size=15))  +
           ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") +
           geom_text(subset(rat_amce, estimate!=0), mapping=aes(label=round(estimate,3)), 
                     parse = TRUE, color="black", size=3, check_overlap = TRUE, 
                     vjust = 1.4) +scale_color_jcolors("pal11") +
        theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_blank(),
        panel.border = element_rect(colour = "black")) +
        labs(x = "Estimated AMCE", y = "")
            
plot(plot_rat_amce_final)

plot_rat_amce_final

plot_rat_amce_final %>% ggsave(filename = "figure_a13.png",
                               width = 8, 
                               height = 8, 
                               dpi = 150, 
                               units = "in", 
                               device='png')

```

# Figure A.14

# Load study 1 data and data for for small cities

```{r}
# Study 1
rat <- read.csv(file="study_1.csv")

rat$residents_allowed_submit <- as.factor(rat$residents_allowed_submit)
rat$projects_allowed <- as.factor(rat$projects_allowed)
rat$support_municipality <- as.factor(rat$support_municipality)
rat$choice_winning_project <- as.factor(rat$choice_winning_project)
rat$outcome_favorability <- as.factor(rat$outcome_favorability)

# Small cities
rat_small_cities <- read.csv(file="study_small_cities.csv")

rat_small_cities$residents_allowed_submit <- as.factor(rat_small_cities$residents_allowed_submit)
rat_small_cities$projects_allowed <- as.factor(rat_small_cities$projects_allowed)
rat_small_cities$support_municipality <- as.factor(rat_small_cities$support_municipality)
rat_small_cities$choice_winning_project <- as.factor(rat_small_cities$choice_winning_project)
rat_small_cities$outcome_favorability <- as.factor(rat_small_cities$outcome_favorability)


```

```{r}

rat <- rat %>% mutate(small_cities=0)

rat_small_cities <- rat_small_cities %>% mutate(small_cities=1)

subgroup_analysis_small_cities <- rbind(rat, rat_small_cities)

subgroup_analysis_small_cities <- subgroup_analysis_small_cities %>%        mutate(small_city=ifelse(small_cities==1, "Yes", "No"))

```

### Comparison small vs big cities

```{r}

subgroup_analysis_small_cities %>% group_by(small_city) %>% summarise(mean_outcome=mean(selected))
    
subgroup_analysis_small_cities$small_city <- as.factor(subgroup_analysis_small_cities$small_city)
    
rat_differences_small_cities <- mm_diffs(subgroup_analysis_small_cities, 
                                selected ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability, 
                                id = ~responseid,
                                h0=-0.02082,
                                by = ~small_city,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?"))

    
rat_differences_small_cities_plot <- plot(rat_differences_small_cities, 
                                          vline=-0.02082,
                                          size=2, 
                                          feature_headers = FALSE)

rat_differences_small_cities_final <- rat_differences_small_cities_plot + 
                          theme(legend.position="none") +
                          theme(axis.text.y = element_text(colour = "black")) +
                          theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
                          theme(text = element_text(size=15)) +
                          ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") +
                          geom_text(rat_differences_small_cities,
                                    mapping=aes(label=round(estimate,3)), 
                                    parse = TRUE, color="black", size=3, check_overlap = TRUE, 
                                    vjust = 1.4) +
                          scale_color_manual(values = dark2) +
                          scale_x_continuous(limits = c(-0.2, 0.2)) +
                          labs(x = "Estimated difference(small-large cities)", y = "") +
                          theme(axis.title.x = element_text(size=12))

```

```{r}
rat_mm_small <- cj(subgroup_analysis_small_cities, 
                   selected ~ 
                   residents_allowed_submit +
                   projects_allowed +
                   support_municipality + 
                   choice_winning_project +
                   outcome_favorability, 
                   id = ~responseid,
                   estimate = "mm",
                   by =  ~small_city,
                   feature_order = c("residents_allowed_submit",
                                     "projects_allowed",
                                     "support_municipality",
                                     "choice_winning_project",
                                     "outcome_favorability"),
                   feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                      projects_allowed = "What projects are allowed?",
                                      outcome_favorability = "Is the outcome favorable?", 
                                      support_municipality = "Does the muninicipality offer any support?", 
                                      choice_winning_project="Who is choosing the winning project?"))


plot_rat_mm_small <- plot(rat_mm_small,
                     size=1, 
                     feature_headers = FALSE,
                     by = "small_city")

plot_rat_mm_final_small <- plot_rat_mm_small +
           theme(legend.position="left") + 
           theme(axis.text.y = element_text(colour = "black")) +
           theme(axis.text.x = element_text(colour = "black")) +
           theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
           theme(text = element_text(size=15))  +
           ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") +
           geom_vline(xintercept = 4.654643	, color="#1B9E77") +
           geom_vline(xintercept = 4.633823, color="#D95F02") +
           scale_color_manual(values = c("#1B9E77", "#D95F02"))

plot(plot_rat_mm_final_small)

figure_small_cities <- ggarrange(plot_rat_mm_final_small, rat_differences_small_cities_final,
                    ncol = 2, widths = c(1.195,1))

plot(figure_small_cities)

ggsave("figure_a14.png", 
      width = 18, 
      height = 8, 
      dpi = 150, 
      units = "in", 
      device='png')

```


# Figure A.15

```{r}

rat_efficiency_final$final_cost <- factor(rat_efficiency_final$final_cost, 
                                          levels = c("Exactly as planned",
                                                     "5% more than planned",
                                                     "25% more than planned",
                                                     "50% more than planned"))


rat_efficiency_final$support_municipality <- factor(rat_efficiency_final$support_municipality, 
                                                    levels= c("No extra support", 
                                                              "Organizing discussion", 
                                                              "Helping to develop the project"))

rat_amce_interaction_efficiency <- cjoint::amce(selected ~ 
                      residents_allowed_submit + 
                      projects_allowed + 
                      support_municipality +
                      choice_winning_project +
                      outcome_favorability +
                      final_cost +
                      outcome_favorability:final_cost,
		                  data = rat_efficiency_final, 
                      na.ignore = TRUE,
		                  cluster = TRUE,
                      respondent.id="responseid")
  
figure_a15 <- plot(rat_amce_interaction_efficiency, 
       colors=dark2,
       label.baseline = TRUE,
       point.size = 0.25,
       xlab="AMCE Estimate") 

figure_a15

```

# Figure A.16

# Load data for study 1 with three levels dependent variable

```{r}

rat_three_levels <- read.csv(file="study_1_three_levels.csv")

rat_three_levels$residents_allowed_submit <- as.factor(rat_three_levels$residents_allowed_submit)
rat_three_levels$projects_allowed <- as.factor(rat_three_levels$projects_allowed)
rat_three_levels$support_municipality <- as.factor(rat_three_levels$support_municipality)
rat_three_levels$choice_winning_project <- as.factor(rat_three_levels$choice_winning_project)
rat_three_levels$outcome_favorability <- as.factor(rat_three_levels$outcome_favorability)

```


## AMCE regression

```{r}
rat_task_1 <- rat_three_levels %>% filter(task==1)

rat_task_1$support_municipality <- factor(rat_task_1$support_municipality, 
                                          levels= c("No extra support", 
                                                    "Organizing discussion", 
                                                    "Helping to develop the project"))

rat_amce <- cj(rat_task_1,
              selected ~ 
                      residents_allowed_submit + 
                      projects_allowed + 
                      support_municipality + 
                      choice_winning_project + 
                      outcome_favorability, 
                      id = ~responseid, 
                      estimate = "amce",
                      #alpha=0.05,
                      # level_order = "descending"
                      feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability"),
                      feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?"))


plot_rat_amce <- plot(rat_amce, 
                     vline=0.0, 
                     size=1, 
                     feature_headers = FALSE)

plot_rat_amce_final <- plot_rat_amce +
           theme(legend.position="none") + 
           theme(axis.text.y = element_text(colour = "black")) +
           theme(axis.text.x = element_text(colour = "black")) +
           theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
           theme(text = element_text(size=15))  +
           ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") +
           geom_text(subset(rat_amce, estimate!=0), mapping=aes(label=round(estimate,3)), 
                     parse = TRUE, color="black", size=3, check_overlap = TRUE, 
                     vjust = 1.4) +
           scale_color_jcolors("pal11") +
           theme(panel.grid.major = element_blank(),
           panel.grid.minor = element_blank(),
           strip.background = element_blank(),
           panel.border = element_rect(colour = "black")) +
           labs(y = "", x = "Estimated AMCE")
            

plot(plot_rat_amce_final)

plot_rat_amce_final %>% ggsave(filename = "figure_a16.png",
                               width = 8, 
                               height = 8, 
                               dpi = 150, 
                               units = "in", 
                               device='png')

```


# Figure A.17

# Load data for study 2 top projects

```{r}

rat_distance_top_projects <- read.csv(file="study_2_top_projects.csv")

rat_distance_top_projects$residents_allowed_submit <- as.factor(rat_distance_top_projects$residents_allowed_submit)
rat_distance_top_projects$projects_allowed <- as.factor(rat_distance_top_projects$projects_allowed)
rat_distance_top_projects$support_municipality <- as.factor(rat_distance_top_projects$support_municipality)
rat_distance_top_projects$choice_winning_project <- as.factor(rat_distance_top_projects$choice_winning_project)
rat_distance_top_projects$project_location <- as.factor(rat_distance_top_projects$project_location)

```



```{r}

rat_distance_top_projects$project_location <- factor(rat_distance_top_projects$project_location,
                                                     levels = c("On one's street",
                                                                "1 street away",
                                                                "4 streets away"))

rat_distance_top_projects$support_municipality <- factor(rat_distance_top_projects$support_municipality, 
                                                         levels= c("No extra support", 
                                                                   "Organizing discussion", 
                                                                   "Helping to develop the project"))

rat_amce_distance_top <- cj(rat_distance_top_projects,
              selected ~ 
              residents_allowed_submit + 
              projects_allowed + 
              support_municipality + 
              choice_winning_project + 
              project_location, 
              id = ~responseid, 
              estimate = "amce",
              feature_order = c("residents_allowed_submit",
                                "projects_allowed",
                                "support_municipality",
                                "choice_winning_project",
                                "project_location"),
              feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                 projects_allowed = "What projects are allowed?",
                                 project_location = "Where is the winning projected located?", 
                                 support_municipality = "Does the muninicipality offer any support?", 
                                 choice_winning_project="Who is choosing the winning project?"))


```

### AMCE figure

```{r}

plot_rat_amce_distance_top <- plot(rat_amce_distance_top, 
                     vline = 0.0, 
                     size = 1, 
                     feature_headers = FALSE)

plot_rat_amce_final_distance_top <- plot_rat_amce_distance_top +
           theme(legend.position="none") + 
           theme(axis.text.y = element_text(colour = "black")) +
           theme(axis.text.x = element_text(colour = "black")) +
           theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
           theme(text = element_text(size=15))  +
           ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") +
           geom_text(subset(rat_amce_distance_top, estimate!=0), mapping=aes(label=round(estimate,3)),
                     parse = TRUE, color="black", size=3, check_overlap = TRUE, 
                     vjust = 1.4) +scale_color_jcolors("pal11") +
          theme(panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            strip.background = element_blank(),
            panel.border = element_rect(colour = "black"))
            
plot(plot_rat_amce_final_distance_top)

plot_rat_amce_final_distance_top %>% ggsave(filename = "figure_a17.png",
                                             width = 8, 
                                             height = 8, 
                                             dpi = 150, 
                                             units = "in", 
                                             device='png')

```


# Figure A.18

```{r}


rat_efficiency_final <- rat_efficiency_final %>% 
  mutate(lower_differences=ifelse(ideology_inequal>4, "Yes",
         ifelse(ideology_inequal<=4, "No", NA)))


rat_efficiency_final$lower_differences <- as.factor(rat_efficiency_final$lower_differences)

rat_efficiency_final$lower_differences <- factor(rat_efficiency_final$lower_differences,
                                                 levels= c("No", "Yes"))


rat_efficiency_final %>% dplyr::group_by(lower_differences) %>% dplyr::summarise(mean_outcome=mean(selected))


rat_efficiency_final <- rat_efficiency_final %>% mutate(profile2=ifelse(profile==1, "Left", "Right"))

rat_efficiency_final$profile2 <- as.factor(rat_efficiency_final$profile2)

rat_efficiency_final$final_cost <- factor(rat_efficiency_final$final_cost, 
                                          levels = c("Exactly as planned",
                                                     "5% more than planned",
                                                     "25% more than planned",
                                                     "50% more than planned"))

rat_efficiency_final$support_municipality <- factor(rat_efficiency_final$support_municipality, 
                                                    levels= c("No extra support", 
                                                              "Organizing discussion", 
                                                              "Helping to develop the project"))

rat_differences_eff <- mm_diffs(rat_efficiency_final, 
                                selected ~ 
                                residents_allowed_submit +
                                projects_allowed +
                                support_municipality + 
                                choice_winning_project +
                                outcome_favorability +
                                final_cost +
                                profile2, 
                                id = ~responseid,
                                h0 = 0.199312,
                                by = ~lower_differences,
                                alpha = 0.05,
                                feature_order = c("residents_allowed_submit",
                                        "projects_allowed",
                                        "support_municipality",
                                        "choice_winning_project",
                                        "outcome_favorability",
                                        "final_cost",
                                        "profile2"),
                                feature_labels = c(residents_allowed_submit = "Who can submit a project?", 
                                         projects_allowed = "What projects are allowed?",
                                         outcome_favorability = "Is the outcome favorable?", 
                                         support_municipality = "Does the muninicipality offer any support?", 
                                         choice_winning_project="Who is choosing the winning project?",
                                         final_cost = "What is the final cost of the project?",
                                         profile2 = "Profile"))

rat_differences_eff_plot <- plot(rat_differences_eff, 
                                    vline = 0.199312, 
                                    size = 2, 
                                    feature_headers = FALSE, 
                                    color = "black")


rat_differences_eff_plot_final <- rat_differences_eff_plot + 
                                   theme(legend.position="none") +
                                   theme(axis.text.y = element_text(colour = "black")) +
                                   theme(plot.title = element_text(hjust = 1.0, vjust = 3)) +
                                   theme(text = element_text(size=15)) +
                                   ggplot2::facet_wrap(~feature, ncol=1, scales = "free_y") +
                                   geom_text(rat_differences_eff,
                                            mapping=aes(label=round(estimate,3)), 
                                            parse = TRUE, color="black", size=3, check_overlap = TRUE,
                                            vjust = 1.4) +
                                   scale_color_manual(values = dark2) +
                                   scale_x_continuous(limits = c(-0.2, 0.6)) +
                                   labs(x = "Estimated difference(decrease-increase)", y = "") +
                                   theme(axis.title.x = element_text(size=12))

rat_differences_eff_plot_final

            
rat_differences_eff_plot_final %>% ggsave(filename = "figure_a17.png",
                               width = 8, 
                               height = 8, 
                               dpi = 150, 
                               units = "in", 
                               device='png')

```


# Table A.3

## Load data and create corpus

```{r}

collection_na <- read.csv(file="text_evaluation.csv")

corpus<-corpus(collection_na,text_field="Q51")

summary(corpus, n=10)

```

## Corpus transformation

```{r}

dtm <- dfm(corpus, 
           tolower = TRUE, 
           stem = TRUE, 
           remove_punct = TRUE, 
           remove_numbers = TRUE,
           remove = c(stopwords("dutch")),
           verbose = TRUE)

dtm_2 <- dfm_remove(dtm, 
                    min_nchar = 2,
                    verbose = TRUE)

dtm_3 <- dfm_trim(dtm_2, 
                  min_docfreq = 2,
                  verbose=TRUE)

```


## LDA topic model: 2 TOPICS

```{r}

rowTotals <- apply(dtm_3 , 1, sum) #Find the sum of words in each Document
dtm.new   <- dtm_3[rowTotals> 0, ] #remove all docs without words

K <- 2
lda10 <- LDA(dtm.new, 
             k = K, 
             method = "Gibbs", 
             control = list(verbose=25L, 
                            seed = 2019, 
                            burnin = 200, 
                            iter = 10000,
                            alpha=0.5))

```

## Getting the top 30 terms per topic

```{r}
terms10 <- get_terms(lda10, 30)
terms10 <- as.data.frame(terms10)

write.csv(terms10, "terms_lda.csv")

round(lda10@gamma[1,], 2)

lda10_topics <- as.data.frame(lda10@gamma)
lda10_topics

sum(lda10_topics$V1)
length(lda10_topics$V1)
sum(lda10_topics$V2)

```

